const Chunk = require("../template")

function testChunkBasic() {
  console.log(
    "1. 基本分块测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5, 6, 7, 8]
      const result = Chunk.chunk(arr, 3)
      const expected = [[1, 2, 3], [4, 5, 6], [7, 8]]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "2. 默认大小分块测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5]
      const result = Chunk.chunk(arr)
      const expected = [[1], [2], [3], [4], [5]]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "3. 空数组分块测试:",
    (() => {
      const arr = []
      const result = Chunk.chunk(arr, 3)
      return result.length === 0
    })()
  )

  console.log(
    "4. 单元素数组分块测试:",
    (() => {
      const arr = [1]
      const result = Chunk.chunk(arr, 3)
      const expected = [[1]]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "5. 大小为0的分块测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5]
      const result = Chunk.chunk(arr, 0)
      return result.length === 0
    })()
  )

  console.log(
    "6. 大小大于数组长度的分块测试:",
    (() => {
      const arr = [1, 2, 3]
      const result = Chunk.chunk(arr, 5)
      const expected = [[1, 2, 3]]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "7. 字符串数组分块测试:",
    (() => {
      const arr = ['a', 'b', 'c', 'd', 'e']
      const result = Chunk.chunk(arr, 2)
      const expected = [['a', 'b'], ['c', 'd'], ['e']]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "8. 对象数组分块测试:",
    (() => {
      const arr = [
        { id: 1, name: 'A' },
        { id: 2, name: 'B' },
        { id: 3, name: 'C' },
        { id: 4, name: 'D' }
      ]
      const result = Chunk.chunk(arr, 2)
      return result.length === 2 && result[0].length === 2 && result[1].length === 2
    })()
  )

  console.log(
    "9. 按数量分块测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5, 6, 7, 8]
      const result = Chunk.chunkByCount(arr, 3)
      return result.length === 3
    })()
  )

  console.log(
    "10. 不可变分块测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5]
      const result = Chunk.chunkImmutable(arr, 2)
      const original = [1, 2, 3, 4, 5]
      return JSON.stringify(arr) === JSON.stringify(original) && result.length === 3
    })()
  )

  console.log(
    "11. this上下文分块测试:",
    (() => {
      const chunkObj = {
        multiplier: 2,
        processChunk: function(arr) {
          const chunks = Chunk.chunk(arr, 2)
          return chunks.length * this.multiplier
        }
      }
      const arr = [1, 2, 3, 4, 5]
      return chunkObj.processChunk(arr) === 6 // 3*2=6
    })()
  )

  console.log(
    "12. 复杂对象this绑定分块测试:",
    (() => {
      const chunkConfigObj = {
        config: { threshold: 2 },
        processWithConfig: function(arr) {
          const chunks = Chunk.chunk(arr, 2)
          return chunks.length >= this.config.threshold
        }
      }
      const arr = [1, 2, 3, 4, 5]
      return chunkConfigObj.processWithConfig(arr) === true
    })()
  )
}

testChunkBasic()
